<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/12/11  14:28
 */
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;

class Withdraw extends Common
{
    /*
	*	构造函数
	*/
    function __construct()
    {
        parent::__construct();
    }

    //提现列表
    public function lists()
    {
        $where = [];

        $pageParam = ['query' =>[]];
        $status = input("status");

        if($status != ''){
            if($status == 'pending_review'){
                $status = 0;
            }
            $where['status']=$status;
            $pageParam['query']['status'] = $status;
            if($status == 0){
                $status = 'pending_review';
            }
        }

        $uid = input('uid');

        if(!empty($uid)){

            $where2['uid'] =$uid; 
        }else{
            $where2 ='';
        }

        $w_status = input('w_status');

        if(!empty($w_status)){

            $where3['status'] = $w_status;
        }else{
            $where3 = 'status=0';
        }

        $list = db('withdraw')
            ->where($where)->where($where2)->order('id desc')->where($where3)->paginate(15,false,$pageParam)->each(function($item,$key){

            /*$item['nickname'] = db('member')->where(['id'=>$item['user_id']])->value('nickname');*/
            switch ($item['status']) {
                case '0':
                    $item['status_name'] = '待审核';
                    break;
                case '1':
                    $item['status_name'] = '通过审核，已到账！';
                    break;
                case '-1':
                    $item['status_name'] = '拒绝提现！';
                    break;
            }

            $user = db('user_account')->where('id',$item['uid'])->find();

            $item['head_url'] = $user['head_url'];

            $item['phone'] = $user['phone'];

            $item['nick_name'] = $user['nick_name'];

            $item['phone'] = $user['phone'];

        /*    $item['nick_name'] = $user['nick_name'];*/

            $item['account'] = $user['account'];

            $item['wx'] = $user['wx'];
            
      /*      $item['create_at'] = date('Y-m-d',$item['create_at']);*/


            $item['create_at'] = date('Y-m-d H:i:s',$item['create_at']);

            return $item;
        });

        $this->assign('page', $list->render());
        $this->assign('withdraw_status', $status);
        $this->assign('list',$list);
        return $this->fetch();
    }


    //提现记录
    public function record()
    {

        $status = input("status");

        if($status != ''){
         /*   if($status == 'pending_review'){
                $status = 0;
            }*/
            $where['status']=$status;
         /*   $pageParam['query']['status'] = $status;
            if($status == 0){
                $status = 'pending_review';
            }*/
            $this->assign('status',$status);
        }else{
            $where = '';
            $this->assign('status',$status);
        }

        $uid = input('uid');

        if(!empty($uid)){
            $where2 ="uid=$uid";
        }else{
            $where2 ='';
        }

        $list = db('withdraw')
            ->where('status!=0')->where($where)->where($where2)->order('id desc')->paginate(15)->each(function($item,$key){

            /*$item['nickname'] = db('member')->where(['id'=>$item['user_id']])->value('nickname');*/
            switch ($item['status']) {
                case '0':
                    $item['status_name'] = '待审核';
                    break;
                case '1':
                    $item['status_name'] = '通过审核，已到账！';
                    break;
                case '-1':
                    $item['status_name'] = '拒绝提现！';
                    break;
            }
            
            $user = db('user_account')->where('id',$item['uid'])->find();

            $item['head_url'] = $user['head_url'];

            $item['phone'] = $user['phone'];

            $item['nick_name'] = $user['nick_name'];

            $item['account'] = $user['account'];

            $item['wx'] = $user['wx'];
            
            $item['create_at'] = date('Y-m-d Y-m-d H:i:s',$item['create_at']);

            return $item;
        });

        /*dump($list);die;*/
        $this->assign('page', $list->render());
  
        $this->assign('list',$list);
        return $this->fetch();
    }

    //提现状态
    public function detail()
    {
        $id = input('id');

        $withdraw = db('withdraw')->where(['id'=>$id])->find();

        if(request()->isAjax()){
            $status = input('status');

            if($status==1){
                $data['uid'] = $withdraw['uid'];
                $data['name'] = $withdraw['name'];
                $data['type'] =1;
                $data['card_number'] =$withdraw['card_num'];
                $data['created_at'] =time();
                db('withdraw_info')->insert($data);
            }

            $bool = db('withdraw')->where(['id'=>$id])->update(['status'=>$status]);

            if(!$bool)$this->error('修改失败!');
            $this->success('修改成功!',url('Withdraw/lists'));
        }

        $this->assign('withdraw',$withdraw);
        return $this->fetch();
    }



    //导出记录
    public function withdraw_export(){

        $data = db('withdraw')->where('status!=0')->select();

        foreach ($data as $key => $value) {

            $user = db('user_account')->where('id',$value['uid'])->find();

            
   
            /*$new[$key]['head_url'] = $user['head_url'];*/

            $new[$key]['account'] = $user['account'];
            $new[$key]['wx'] = $user['wx'];
            $new[$key]['name'] = $value['name'];
            $new[$key]['card_num'] = $value['card_num'];

            $new[$key]['phone'] = $user['phone'];

            $new[$key]['nick_name'] = $user['nick_name'];

            switch ($value['status']) {
            
                case '1':
                    $new[$key]['status'] = '通过审核';
                break;
                case '-1':
                    $new[$key]['status'] = '未通过审核';
                break;

                default:
                    $new[$key]['status'] ='';
                    break;
            }

            $new[$key]['time'] = date("Y-m-d H:i",$value['create_at']);

        }


        $headArr = array("会员账号",'微信','姓名','收款账号',"电话号码","昵称",'审核结果',"审核时间");

        if(empty($new)){
            $this->error('数据为空');
        }
        
        $data3 =array_filter($new);
        
         $filename='提现记录-'.date("Y-m-d H:i:s");
        (new Export())->ImportExcel($filename,$headArr,$data3);
    }
}